<?php
class NodeModel extends Model {
	public $list;
	public $auth_module;
	public function _initialize(){
		parent::_initialize();
		$this->auth_module= explode(',', C('NOT_AUTH_MODULE'));
	}
	public function getList($pid=0,$i=0,$id=0){
        $map['pid']=$pid;
        if($id!=0){
            $map['id']=$id;    
        }
		$re = $this->where($map)->select();
		if(false == $re) return null;
		foreach ($re as $v){
			$v['cl']=$i;
		    $this->list[]=$v;
			array_merge($this->list,$this->getList($v['id'],$i+1));
		}
		return $this->list;
	}
		public function getListToArray($role_id=0,$pid=0, $node = ''){
		$nodeMap = '';
		if($node != ''){
			$nodeMap = ' and n.id in('.$node.')';
		}
		$re=$this->alias('n')->join('left join __ACCESS__ a ON a.node_id= n.id and a.role_id='.$role_id)->Field('n.*,a.role_id')->where('n.pid='.$pid.$nodeMap)->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$re[$k]['sub']=$this->getListToArray($role_id,$v['id'], $node);
			}
			return $re;
		}
	}
	public function getListToArray1($pid=0,$deep=0,$arr=array()){
		$re=$this->where(array('pid'=>$pid))->select();
		if($re!==false){
			$deep++;
			foreach($re as $k=>$v){
				$v['deep']=$deep;
				$arr[]=$v;
				$arr=$this->getListToArray1($v['id'],$deep,$arr);
			}
			return $arr;
		}
	}
    public function getListToArray5($role_id=0,$pid=0, $node = '', $id){
		$nodeMap = ' ';
		if($node != ''){
			$nodeMap = ' and n.id in('.$node.')';
		}
        if($id!=0){
            $nodeMap.=' and n.id='.$id;   
        }
		$re=$this->alias('n')->join('left join __MEMBER_ACCESS__ a ON a.node_id= n.id and a.role_id='.$role_id)->Field('n.*,a.role_id')->where('n.status=1 and n.pid='.$pid.$nodeMap)->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$id = 0;
				$re[$k]['sub']=$this->getListToArray5($role_id,$v['id'], $node, $id);
			}
			return $re;
		}
	}
	
    public function getListToArray2($role_id=0,$pid=0, $node = '', $id){
		$nodeMap = ' ';
		if($node != ''){
			$nodeMap = ' and n.id in('.$node.')';
		}
        if($id!=0){
            $nodeMap.=' and n.id='.$id;   
        }
		$re=$this->alias('n')->join('left join __MEMBER_ACCESS__ a ON a.node_id= n.id and a.role_id='.$role_id)->Field('n.*,a.role_id')->where('n.status=1 and n.pid='.$pid.$nodeMap)->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$id = 0;
				if($v['level']==2&&in_array($v['name'], $this->auth_module)){
					unset($re[$k]);
				}else{
					$re[$k]['sub']=$this->getListToArray2($role_id,$v['id'], $node, $id);	
				};
				
			}
			return $re;
		}
	}
}

?>